package com.inforich.projectManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.dto.ProjectApproveDTO;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.projectManage.mapper.ProjectRubbishReportMapper;
import com.inforich.projectManage.domain.ProjectRubbishReport;
import com.inforich.projectManage.service.IProjectRubbishReportService;

/**
 * 固废排放计划申报Service业务层处理
 *
 * @author hjm
 * @date 2022-10-29
 */
@Service
public class ProjectRubbishReportServiceImpl extends
    ServiceImpl<ProjectRubbishReportMapper, ProjectRubbishReport> implements
    IProjectRubbishReportService {

  @Override
  public Page<ProjectRubbishReport> pageProjectRubbishReport(Page<ProjectRubbishReport> page,
      Map<String, Object> map) {
    return baseMapper.pageProjectRubbishReport(page, map);
  }

  @Override
  public List<ProjectRubbishReport> listProjectRubbishReport(Map<String, Object> map) {
    return baseMapper.listProjectRubbishReport(map);
  }

  @Override
  public ProjectRubbishReport getProjectRubbishReportById(String id) {
    return baseMapper.selectProjectRubbishReportById(id);
  }

  @Override
  public ProjectRubbishReport getProjectRubbishReportByProjectId(String projectId) {
    return baseMapper.getProjectRubbishReportByProjectId(projectId);
  }

  @Override
  public AjaxResult approve(ProjectApproveDTO approveDTO) {
    Integer approvalStatus =
        ApproveEnum.APPROVE_STATUS_SUCCESS.equals(approveDTO.getStatus()) ?
            ApproveEnum.PASS.getDbData() : ApproveEnum.REJECT.getDbData();
    boolean result = update(new LambdaUpdateWrapper<ProjectRubbishReport>()
        .set(ProjectRubbishReport::getApprovalStatus, approvalStatus)
        .set(ProjectRubbishReport::getApprovalDate, new Date())
        .set(ProjectRubbishReport::getApprovalDesc, approveDTO.getApprovalDesc())
        .eq(ProjectRubbishReport::getId, approveDTO.getId())
        .eq(ProjectRubbishReport::getApprovalStatus, ApproveEnum.REVIEW.getDbData()));
    return result ? AjaxResult.success() : AjaxResult.error();
  }

  @Override
  public int countByProjectId(String projectId) {
    return this.count(new LambdaQueryWrapper<ProjectRubbishReport>()
        .eq(ProjectRubbishReport::getProjectId, projectId));
  }

  @Override
  public void removeByProjectId(String projectId) {
    remove(new LambdaQueryWrapper<ProjectRubbishReport>()
        .eq(ProjectRubbishReport::getProjectId, projectId));
  }

}
